<?php

function registerStudent($lastname, $firstname, $email, $phoneNumber) {
    unsDepartement_addStudent($lastname, $firstname, $email, $phoneNumber);
}

function registerStudentbyCSV($file) {
    $handle = fopen("$file", "r");
    while (($data = fgetcsv($handle, 4096, ";")) !== FALSE) {
        unsDepartement_addStudent($data[0], $data[1], $data[2], $data[3]);
    }
}

function displayListStudent() {

    global $babBody;

    class displayListStudentClass {

        function displayListStudentClass() {

            $this->lastnameLabel = bab_translate("Lastname", $GLOBALS['babAddonFolder']);
            $this->firstnameLabel = bab_translate("Firstname", $GLOBALS['babAddonFolder']);
            $this->phoneNumberLabel = bab_translate("Phone Number", $GLOBALS['babAddonFolder']);
            $this->emailLabel = bab_translate("Email", $GLOBALS['babAddonFolder']);

            $this->listStudent = bab_getGroupsMembers(unsDepartement_getGroupIDByName('uns_student'));
            $this->listLength = count($this->listStudent);

            $this->bred = true;
        }

        function nextrow() {
            static $k = 0;

            $this->bred = !$this->bred;

            if ($k < $this->listLength) {
                $this->student = bab_getUserInfos($this->listStudent[$k]['id']);
                $k++;
                return true;
            } else {
                return false;
            }
        }

    }

    $obj = new displayListStudentClass();
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "student.html", "list"));
}

function displayAddStudentForm() {

    global $babBody;

    class displayAddStudentFormClass {

        function displayAddStudentFormClass() {

            $this->lastnameLabel = bab_translate("Lastname", $GLOBALS['babAddonFolder']);
            $this->firstnameLabel = bab_translate("Firstname", $GLOBALS['babAddonFolder']);
            $this->phoneNumberLabel = bab_translate("Phone Number", $GLOBALS['babAddonFolder']);
            $this->emailLabel = bab_translate("Email", $GLOBALS['babAddonFolder']);
            $this->addStudentButton = bab_translate("Add Student", $GLOBALS['babAddonFolder']);
            $this->CSVLabel = bab_translate("CSV", $GLOBALS['babAddonFolder']);
        }

    }

    $obj = new displayAddStudentFormClass();
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "student.html", "addForm"));
}

function displayAddStudentCSVForm() {

    global $babBody;

    class displayAddStudentCSVFormClass {

        function displayAddStudentCSVFormClass() {
            global $babDB;
            $this->CSVLabel = bab_translate("CSV", $GLOBALS['babAddonFolder']);
        }

    }

    $obj = new displayAddStudentCSVFormClass();
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "student.html", "addForm2"));
}



if (isset($_POST['addStudentSubmit'])) {
    if (!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['email']) && !empty($_POST['phoneNumber'])) {
        registerStudent($_POST['lastname'], $_POST['firstname'], $_POST['email'], $_POST['phoneNumber']);
    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
} else if (isset($_FILES['file'])) {
    $file = $_FILES['file']['tmp_name'];
    registerStudentbyCSV($file);
}
    
    
$babBody->addItemMenu("listStudent", bab_translate("List Student", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=student&do=listStudent");
    
if (bab_isAccessValid('unsDepartement_rights_groups', 1002)) {
    $babBody->addItemMenu("addStudent", bab_translate("Add Student", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=student&do=addStudent");
    $babBody->addItemMenu("addStudentCSV", bab_translate("Add Student CSV", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=student&do=addStudentCSV");
}

$do = bab_rp('do','listStudent');

switch ($do) {
    case "listStudent":
        displayListStudent();
        break;
    case "addStudentCSV":
        displayAddStudentCSVForm();
        break;
    case "addStudent":
        displayAddStudentForm();
    default :
        displayListStudent();
        break;
}

$babBody->setCurrentItemMenu($do);

?>
*/
